//
//  ShufflingView.m
//  FoodCorner
//
//  Created by Summer on 16/2/28.
//  Copyright © 2016年 zhangchao. All rights reserved.
//

#import "ShufflingView.h"
#import <SDWebImage/UIImageView+WebCache.h>
#define kImageOffsetAndWidth [UIScreen mainScreen].bounds.size.width

@interface ShufflingView ()

@end

@implementation ShufflingView

-(NSArray *)ImgArray
{
    if (!_ImgArray) {
        _ImgArray = [NSArray array];
    }
    return _ImgArray;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self addAllViews];
        [self  addTimer];
        self.scrollView.delegate = self;
    }
    return self;
}
- (void)addAllViews
{
    
    self.ImgArray = @[@"http://ibaby.ipadown.com/uploads/food-thumb/rgmbd2kij4q.jpg"
                     ,@"http://ibaby.ipadown.com/uploads/food-thumb/t1zts3qnweh.jpg"
                     ,@"http://ibaby.ipadown.com/uploads/food-thumb/00m5nmxfeqi.jpg"
                     ,@"http://ibaby.ipadown.com/uploads/food-thumb/icfzisszj35.jpg"
                     ,@"http://ibaby.ipadown.com/uploads/food-thumb/xvkdtxrmkgz.jpg"];
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kImageOffsetAndWidth, self.frame.size.height)];
    self.scrollView.contentSize = CGSizeMake(kImageOffsetAndWidth * 3, 100);
    self.scrollView.contentOffset = CGPointMake(kImageOffsetAndWidth, 0);
    [self addSubview:_scrollView];
    self.leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kImageOffsetAndWidth, self.frame.size.height)];
    [_scrollView addSubview:_leftImageView];
    
    self.centerImageView  = [[UIImageView alloc]initWithFrame:CGRectMake(kImageOffsetAndWidth, 0, kImageOffsetAndWidth, self.frame.size.height)];
    [_scrollView addSubview:_centerImageView];
    _scrollView.backgroundColor = [UIColor clearColor];
    _scrollView.alpha = 0.8;
    self.rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(kImageOffsetAndWidth * 2, 0, kImageOffsetAndWidth, self.frame.size.height)];
    
    [_scrollView addSubview:_rightImageView];
    
    
    [_leftImageView sd_setImageWithURL:[NSURL URLWithString:self.ImgArray[0]]];
    
    [_centerImageView sd_setImageWithURL:[NSURL URLWithString:self.ImgArray[1]]];
    
    [_rightImageView sd_setImageWithURL:[NSURL URLWithString:self.ImgArray[2]]];
    
    self.pageControl = [[UIPageControl alloc]init];
    
    _pageControl.bounds = CGRectMake(0, 0, CGRectGetWidth(self.frame), 50);
    
    _pageControl.center = CGPointMake(self.scrollView.bounds.size.width / 2, self.scrollView.bounds.size.height - 15);
    
    _pageControl.currentPage = _currentImageIndex;
    
    _pageControl.numberOfPages = 5;
    
    [self addSubview: self.pageControl];
    
}
- (void)addTimer
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
- (void)removeTimer
{
    [self.timer invalidate];
    self.timer = nil;
}
- (void)nextImage
{
    [UIView animateWithDuration:5.0 animations:^{
        self.scrollView.contentOffset = CGPointMake(kImageOffsetAndWidth * 2, 0);
    }];
    
    int leftImageIndex,rightImageIndex;
    
    self.currentImageIndex = (self.currentImageIndex + 1) % (self.ImgArray.count);
    
    [self.centerImageView sd_setImageWithURL:[NSURL URLWithString:self.ImgArray[self.currentImageIndex]]];
    
    rightImageIndex = (self.currentImageIndex + 1) % (self.ImgArray.count);
    leftImageIndex = (self.currentImageIndex + (int)(self.ImgArray.count -1)) % (self.ImgArray.count);
    
    [_leftImageView sd_setImageWithURL:[NSURL URLWithString:self.ImgArray[leftImageIndex]]];
   // NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",self.ImgArray[rightImageIndex]]];
    
    [_rightImageView sd_setImageWithURL:[NSURL URLWithString:self.ImgArray[rightImageIndex]]];
    
    self.scrollView.contentOffset = CGPointMake([UIScreen mainScreen].bounds.size.width , 0);
    
    self.pageControl.currentPage = _currentImageIndex;
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self reloadImage];
    self.scrollView.contentOffset = CGPointMake([UIScreen mainScreen].bounds.size.width , 0);
    
    self.pageControl.currentPage = _currentImageIndex;
}
-(void)reloadImage
{
    CGPoint offset = self.scrollView.contentOffset;
    
    if (offset.x > [UIScreen mainScreen].bounds.size.width) {
        self.currentImageIndex = (self.currentImageIndex + 1) % (self.ImgArray.count);
    }else if (offset.x <[UIScreen mainScreen].bounds.size.width)
    {
        self.currentImageIndex = (self.currentImageIndex + (int)(self.ImgArray.count -1)) % (self.ImgArray.count);
    }
    
    int leftImageIndex,rightImageIndex;
    
    self.currentImageIndex = (self.currentImageIndex + 1) % (self.ImgArray.count);
    
    [self.centerImageView sd_setImageWithURL:[NSURL URLWithString:self.ImgArray[self.currentImageIndex]]];
    
    rightImageIndex = (self.currentImageIndex + 1) % (self.ImgArray.count);
    leftImageIndex = (self.currentImageIndex + (int)(self.ImgArray.count -1)) % (self.ImgArray.count);
    
    [_leftImageView sd_setImageWithURL:[NSURL URLWithString:self.ImgArray[leftImageIndex]]];
    // NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",self.ImgArray[rightImageIndex]]];
    
    [_rightImageView sd_setImageWithURL:[NSURL URLWithString:self.ImgArray[rightImageIndex]]];
    
    self.scrollView.contentOffset = CGPointMake([UIScreen mainScreen].bounds.size.width , 0);
    
    self.pageControl.currentPage = _currentImageIndex;
    
}

@end
